import React from 'react';
import {Modal,Form,Input,Icon } from 'antd';
import {connect} from 'dva';
const  FormItem=Form.Item;
const formItemLayout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 8 },
};


class EditRoleModalUI extends React.Component {
  state = {
    editModalVisible:false,
  };

  componentWillUpdate(nextProps,nextState) {
    //组件的props改变后触发此函数
    this.state.editModalVisible = true;
  }
  handleOk = () => {
      this.props.form.validateFieldsAndScroll((errors, values) => {
        if (errors) {

        } else {
          this.props.dispatch({
            type: 'role/addOrUpdateRole',
            payload:values
          });
          this.state.editModalVisible = false;
          this.props.form.resetFields();
          this.setState(this.state);
        }
      });
  };
  handleCancel = () => {
    this.state.editModalVisible = false;
    this.props.form.resetFields();
    this.setState(this.state);
  };

  render() {
    return(
      <Modal title="编辑角色" visible={this.state.editModalVisible} onOk={()=>{this.handleOk()}}
             onCancel={()=>{this.handleCancel()}} okText="确认" cancelText="取消" maskClosable={false}
      >
        <Form>
          {
            this.props.form.getFieldDecorator('id',{
              initialValue:this.props.editRow.id,
              rules: [
                {
                  required: true,
                },
              ],
            })(<Input type="hidden" />)
          }
          <FormItem {...formItemLayout}  label="角色名" >
            {
              this.props.form.getFieldDecorator('name', {
                initialValue:this.props.editRow.name,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Input  placeholder="角色名" size="large"/>)
            }
          </FormItem>
        </Form>
      </Modal>

    );
  }
}

export default connect()(Form.create()(EditRoleModalUI));
